package com.sk.sourcecircle.activity.home.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

import com.alibaba.fastjson.JSON;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.sk.sourcecircle.R;
import com.sk.sourcecircle.activity.find.act.SatisfyTaskDetailsActvitiy;
import com.sk.sourcecircle.activity.find.act.TaskDetailsActivity;
import com.sk.sourcecircle.activity.home.activity.EventDetailsActivity;
import com.sk.sourcecircle.activity.home.activity.PersonDetailsActivity;
import com.sk.sourcecircle.adapter.JonedEventAdapter;
import com.sk.sourcecircle.base.BaseFragment;
import com.sk.sourcecircle.entity.EventListBean;
import com.sk.sourcecircle.utils.HttpAddress;
import com.sk.sourcecircle.utils.MD5;
import com.sk.sourcecircle.utils.MyUtils;
import com.sk.sourcecircle.view.EasyLoadingMoreView;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by sk on 2017/12/27.
 */

public class OtherJoinedEventFragment extends BaseFragment implements BaseQuickAdapter.OnItemClickListener, BaseQuickAdapter.RequestLoadMoreListener {
    @BindView(R.id.data_ryc)
    RecyclerView dataRyc;
    private int page = 1;

    private JonedEventAdapter adapter;
    private List<EventListBean.DataBean> allDataList = new ArrayList<>();
    private boolean isLoadMore = false;
    private int id;

    @Override
    protected int getContentView() {
        return R.layout.layout_recycler;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        dataRyc.setLayoutManager(new LinearLayoutManager(mContext));
        adapter = new JonedEventAdapter(R.layout.item_joined_event, allDataList);
        dataRyc.setAdapter(adapter);
        EasyLoadingMoreView loadMoreView = new EasyLoadingMoreView();
        adapter.setLoadMoreView(loadMoreView);
        View emptyView = getLayoutInflater().inflate(R.layout.empty_view, null);
        adapter.setEmptyView(emptyView);
        adapter.setOnItemClickListener(this);
        adapter.setOnLoadMoreListener(this, dataRyc);

        if (getActivity() instanceof PersonDetailsActivity) {
            id = ((PersonDetailsActivity) getActivity()).getUser_id();
            getJoinedEvent(page);
        }

    }

    public void getJoinedEvent(int page) {
        String path = HttpAddress.TestIp + HttpAddress.GetJoinedEvent;
        RequestParams requestParams = new RequestParams(path + id);
        long timeStamp = System.currentTimeMillis();
        requestParams.addBodyParameter("timestamp", String.valueOf(timeStamp));
        requestParams.addBodyParameter("sign", MD5.md5(timeStamp + "_yuanquan"));
        requestParams.addBodyParameter("token", MyUtils.getToken(mContext));
        requestParams.addBodyParameter("page", String.valueOf(page));
        x.http().get(requestParams, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Log.v("getJoinedEvent", result);
                EventListBean eventListBean = JSON.parseObject(result, EventListBean.class);
                List<EventListBean.DataBean> dataBeanList = eventListBean.getData();
                allDataList.addAll(dataBeanList);
                if (allDataList.size() < 10) {
                    adapter.setEnableLoadMore(false);
                } else {
                    adapter.setEnableLoadMore(true);
                }
                if (isLoadMore) {
                    adapter.loadMoreComplete();
                    if (dataBeanList.size() == 0) {
                        adapter.loadMoreEnd();
                    }
                    isLoadMore = false;
                }
                adapter.notifyDataSetChanged();

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Log.v("getJoinedEvent", ex.toString());
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        if (allDataList.get(position).getType() == 0) {
            Intent intent = new Intent(mContext, EventDetailsActivity.class);
            intent.putExtra("id", allDataList.get(position).getActivity_id());
            startActivity(intent);
        } else if (allDataList.get(position).getType() == 1 && allDataList.get(position).getAc_status() == 0) {
            Intent intent = new Intent(mContext, TaskDetailsActivity.class);
            intent.putExtra("id", allDataList.get(position).getActivity_id());
            startActivity(intent);
        } else if (allDataList.get(position).getType() == 1 && allDataList.get(position).getAc_status() == 1) {
            Intent intent = new Intent(mContext, SatisfyTaskDetailsActvitiy.class);
            intent.putExtra("id", allDataList.get(position).getActivity_id());
            startActivity(intent);
        }
    }

    @Override
    public void onLoadMoreRequested() {
        isLoadMore = true;
        page++;
        getJoinedEvent(page);
    }
}
